package com.platform.framework.controller.rights;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
/****
 * 网易云信发送短信
 * @author 李增冰
 *
 */
public class SMSUtil {
	private static final String SERVER_URL="https://api.netease.im/sms/sendtemplate.action";//发送验证码的请求路径URL
    private static final String APP_KEY="ea1b2443ccbf15ec52db4c93ef25bab8";//网易云信分配的账号
    private static final String APP_SECRET="f80567c7133a";//网易云信分配的密钥
    private static final String NONCE="123456";//随机数
	public  String sendMessage(String mobile) {
		String result="error";
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(SERVER_URL);
		String curTime = String.valueOf((new Date()).getTime() / 1000L);
		String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE ,curTime);//参考 计算CheckSum的java代码
        // 设置请求的header
        httpPost.addHeader("AppKey", APP_KEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        try {
        	// 设置请求的参数
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("templateid", "3050620"));//模板编号
            nvps.add(new BasicNameValuePair("mobiles", "[\""+mobile+"\"]"));//手机号
            //nvps.add(new BasicNameValuePair("params", "[\"【中公高科】\"]"));//变量参数
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

            // 执行请求
            HttpResponse response = httpclient.execute(httpPost);

            // 执行结果
            String data = EntityUtils.toString(response.getEntity(), "utf-8");
            Map obj = (Map) JSON.parse(data);
            String code = obj.get("code").toString();
            if (code.equals("200")){
            	result= "success";
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
        return result;
	}
	static class CheckSumBuilder {
		// 计算并获取CheckSum
	    public static String getCheckSum(String appSecret, String nonce, String curTime) {
	        return encode("sha1", appSecret + nonce + curTime);
	    }

	    // 计算并获取md5值
	    public static String getMD5(String requestBody) {
	        return encode("md5", requestBody);
	    }

	    private static String encode(String algorithm, String value) {
	        if (value == null) {
	            return null;
	        }
	        try {
	            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
	            messageDigest.update(value.getBytes());
	            return getFormattedText(messageDigest.digest());
	        } catch (Exception e) {
	            throw new RuntimeException(e);
	        }
	    }
	    private static String getFormattedText(byte[] bytes) {
	        int len = bytes.length;
	        StringBuilder buf = new StringBuilder(len * 2);
	        for (int j = 0; j < len; j++) {
	            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
	            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
	        }
	        return buf.toString();
	    }
	    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
	            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
	}
	/**
	 * 短信测试
	 *//*
	public static void main(String[] args) {
		SMSUtil smsUtil = new SMSUtil();
		String result = smsUtil.sendMessage("13807031023");
	}*/
}
